import request from '@/utils/request'
import { Message } from 'element-ui'
import {SERVER_URL,SYSTEM_ERROR} from '@/utils/common'
import axios from 'axios';
let login_instance = axios.create({
  headers: {'content-type': 'application/x-www-form-urlencoded'},
  withCredentials: true,
  baseURL: process.env.BASE_API, // api的base_url
  auth:{
    username: 'webapp',
    password: 'webapp_secret'
  }
});


// respone interceptor
login_instance.interceptors.response.use(
  response => response,

  error => {
    Message({
      message: error.response.data.msg?error.response.data.msg: SYSTEM_ERROR,
      type: 'error',
      duration: 2 * 1000
    })
    return Promise.reject(error)
  })







export const login = params => {
  return login_instance.post(SERVER_URL.AUTH_SERVER+`/oauth/token?grant_type=password&username=`
    +params.username+`&password=`+params.password);
};




export function loginByUsername(username, password) {
  const data = {
    username,
    password
  }
  return request({
    url: '/login/login',
    method: 'post',
    data
  })
}

export function logout() {
  return request({
    url: '/login/logout',
    method: 'post'
  })
}

export function getUserInfoByUserNameOrId(userName) {
  return request({
    url: SERVER_URL.SYSTEM_SERVER+`/user/getUserInfoByUserNameOrId?id=&userName=`+userName,
    method: 'get'
  })
}

